package com.java110.mall.shop.bmo.shopCommunity.impl;

import com.java110.dto.community.CommunityDto;
import com.java110.dto.shop.ShopCommunityDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.shop.IShopCommunityInnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.intf.user.IWuyeV1InnerServiceSMO;
import com.java110.mall.shop.bmo.shopCommunity.IGetShopCommunityBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getShopCommunityBMOImpl")
public class GetShopCommunityBMOImpl implements IGetShopCommunityBMO {

    @Autowired
    private IShopCommunityInnerServiceSMO shopCommunityInnerServiceSMOImpl;


    @Autowired
    private IWuyeV1InnerServiceSMO wuyeV1InnerServiceSMOImpl;



    @Autowired
    private IStoreV1InnerServiceSMO storeInnerServiceSMOImpl;

    /**
     * @param shopCommunityDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ShopCommunityDto shopCommunityDto) {


        int count = shopCommunityInnerServiceSMOImpl.queryShopCommunitysCount(shopCommunityDto);

        List<ShopCommunityDto> shopCommunityDtos = null;
        if (count > 0) {
            shopCommunityDtos = shopCommunityInnerServiceSMOImpl.queryShopCommunitys(shopCommunityDto);
            freshShopImg(shopCommunityDtos);
            freshStore(shopCommunityDtos);
        } else {
            shopCommunityDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopCommunityDto.getRow()), count, shopCommunityDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    @Override
    public ResponseEntity<String> getFreeCommunitys(CommunityDto communityDto) {
        //查询商铺入驻的小区
        ShopCommunityDto shopCommunityDto = new ShopCommunityDto();
        shopCommunityDto.setShopId(communityDto.getShopId());
        List<ShopCommunityDto> shopCommunityDtos = shopCommunityInnerServiceSMOImpl.queryShopCommunitys(shopCommunityDto);
        List<String> communityIds = new ArrayList<>();
        if (!ListUtil.isNull(shopCommunityDtos)) {
            for (ShopCommunityDto tmpShopCommunityDto : shopCommunityDtos) {
                communityIds.add(tmpShopCommunityDto.getCommunityId());
            }
            communityDto.setNotInCommunityId(communityIds.toArray(new String[communityIds.size()]));
        }
        communityDto.setState("1100"); //1100表示审核通过状态
        List<CommunityDto> communityDtos = wuyeV1InnerServiceSMOImpl.getFreeCommunitys(communityDto);
        return ResultVo.createResponseEntity(communityDtos);
    }


    /**
     * 刷入商户信息
     *
     * @param shopDtos
     */
    private void freshStore(List<ShopCommunityDto> shopDtos) {

        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        List<String> storeIds = new ArrayList<>();
        for (ShopCommunityDto shopDto : shopDtos) {
            storeIds.add(shopDto.getStoreId());
        }

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreIds(storeIds.toArray(new String[storeIds.size()]));
        List<StoreDto> storeDtos = storeInnerServiceSMOImpl.queryStores(storeDto);

        for (ShopCommunityDto shopDto : shopDtos) {
            for (StoreDto tmpStoreDto : storeDtos) {
                if (shopDto.getStoreId().equals(tmpStoreDto.getStoreId())) {
                    shopDto.setStoreName(tmpStoreDto.getName());
                    shopDto.setStoreTel(tmpStoreDto.getTel());
                    shopDto.setStoreAddress(tmpStoreDto.getAddress());
                }
            }
        }
    }

    /**
     * @param shopDtos
     */
    private void freshShopImg(List<ShopCommunityDto> shopDtos) {
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (ShopCommunityDto shopDto : shopDtos) {
            shopDto.setShopLogo(imgUrl + shopDto.getShopLogo());
        }
    }

}
